import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import i18n from "../../../../i18n";

const js3500 = {
    namespaced: true,
    state: {
        dataSource: [],

        loadTable: false,
        // 选中行
        selectRow: {},
        // 新增编辑弹窗表单数据
        capForm: {
            name: '',
            desc: '',
            startTime: '',
            endTime: '',
        },
        capvisible: false,
        isFlag: null,
        userList: [],
        dataPagination:{
            pageSize:13,
            size:'small',
            total: 0,
            showTotal: (total, range) => {
                return i18n.t('menuItem.total')+` ${total} `+i18n.t('menuItem.term')
            },
        }
    },
    getters: {},
    mutations: {
        setDataPagination(state, val) {
            state.dataPagination = val;
        },
        // 查询table数据
        queryAnnounce(state) {
            let codeInTrx = {
                trxId: "sysNotice",
                actionFlg: "Q",
            };
            state.dataSource = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSource = outTrx.oaryA.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                    state.dataPagination.total = outTrx.tblCnt;
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        // 点击新增按钮
        addData(state) {
            state.isFlag = 'ADD'
            js3500.mutations.cleanForm(state)
            state.capvisible = true
        },
        // 表格行数据
        customRow(state, obj) {
            state.selectRow = {
                id: obj.id,
                name: obj.title,
                desc: obj.content,
                startTime: obj.startTime,
                endTime: obj.endTime,
            }
        },
        // 点击编辑按钮
        updateData(state, val) {
            state.isFlag = 'EDIT'
            state.capForm = {
                name: val.title,
                desc: val.content,
                startTime: val.startTime,
                endTime: val.endTime,
            }
            state.capvisible = true
        },
        // 删除确认
        deleteData(state, val) {

            let codeInTrx = {
                trxId: "sysNotice",
                actionFlg: "D",
                id: state.selectRow.id,
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    message.success('Operation successful')
                    js3500.mutations.queryAnnounce(state)
                }
            }).finally(() => {
            });
        },
        // 新增/編輯弹窗确认
        caphandleOk(state) {
            js3500.mutations.addAndUpdate(state, state.isFlag)
            state.capvisible = false
        },
        // 新增/編輯接口
        addAndUpdate(state, val) {
            if (val === 'EDIT') {
                let codeInTrx = {
                    trxId: "sysNotice",
                    actionFlg: "U",
                    id: state.selectRow.id,
                    title: state.capForm.name,
                    content: state.capForm.desc,
                    startTime: state.capForm.startTime,
                    endTime: state.capForm.endTime,
                };
                SendMsg.sendPostTrx(codeInTrx).then((res) => {
                    let outTrx = res.data;
                    if ('0000000' === outTrx.rtnCode) {
                        message.success('Operation successful')
                        js3500.mutations.queryAnnounce(state)
                        js3500.mutations.cleanForm(state)
                    }
                }).finally(() => {
                });
            } else if (val === 'ADD') {
                let codeInTrx = {
                    trxId: "sysNotice",
                    actionFlg: "A",

                    title: state.capForm.name,
                    content: state.capForm.desc,
                    startTime: state.capForm.startTime,
                    endTime: state.capForm.endTime,
                };
                // console.log(codeInTrx,'新增参数')
                SendMsg.sendPostTrx(codeInTrx).then((res) => {
                    let outTrx = res.data;
                    //  console.log(outTrx,'查询新增数据')
                    if ('0000000' === outTrx.rtnCode) {
                        message.success('Operation successful')
                        js3500.mutations.queryAnnounce(state)
                        js3500.mutations.cleanForm(state)
                    }
                }).finally(() => {
                });
            } else {
                return
            }

        },
        // 新增编辑弹窗取消
        caphandleCancel(state) {
            state.capvisible = false
            js3500.mutations.cleanForm(state)
        },
        // 清空弹窗数据
        cleanForm(state) {
            state.capForm = {
                name: '',
                desc: '',
                startTime: '',
                endTime: '',
            }
        },
        // 查询用户信息列表
        queryUserList(state) {
            let intrx = {
                trxId: "sysUser",
                actionFlg: "QU",
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                //  console.log(outTrx,'查询table数据')
                if ('0000000' === outTrx.rtnCode) {
                    state.userList = outTrx.oaryB
                }
            })
        }
    },
    action: {},
}


export default js3500;
